import Vue from 'vue'
import VueRouter from 'vue-router'
import Store from '@/store/index'
import jwtToken from '@/helpers/jwt'

Vue.use(VueRouter)

let routes = [
  {
    path: '/',
    name: 'home',
    component: resolve => require(['@/components/Home.vue'], resolve),
    meta: {}
  },
  {
    path: '/login',
    name: 'login',
    component: resolve => require(['@/components/LoginForm'], resolve),
    meta: {requiresGuest: true}
  },
  {
    path: '/categories',
    name: 'categories',
    component: resolve => require(['@/components/Categories/Index'], resolve),
    meta: {}
  },
  {
    path: '/polls',
    name: 'polls.index',
    component: resolve => require(['@/components/polls/Index'], resolve),
    meta: {}
  },
  {
    path: '/polls/:poll/edit',
    name: 'polls.edit',
    component: resolve => require(['@/components/polls/edit'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/polls/create',
    name: 'polls.create',
    component: resolve => require(['@/components/polls/create'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/polls/:poll/votes',
    name: 'votes.index',
    component: resolve => require(['@/components/votes/Index'], resolve),
    meta: {}
  },
  {
    path: '/polls/:poll/votes/:vote/edit',
    name: 'votes.edit',
    component: resolve => require(['@/components/votes/edit'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/polls/:poll/votes/create',
    name: 'votes.create',
    component: resolve => require(['@/components/votes/create'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/votes/:vote/answers',
    name: 'votes.answers.index',
    component: resolve => require(['@/components/votes/answers/index'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/votes/:vote/answers/create',
    name: 'votes.answers.create',
    component: resolve => require(['@/components/votes/answers/create'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/votes/:vote/voteInfos',
    name: 'voteInfos.index',
    component: resolve => require(['@/components/voteInfos/Index'], resolve),
    meta: {}
  },
  {
    path: '/votes/:vote/voteInfos/:voteInfo/edit',
    name: 'voteInfos.edit',
    component: resolve => require(['@/components/voteInfos/edit'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/votes/:vote/voteInfos/create',
    name: 'voteInfos.create',
    component: resolve => require(['@/components/voteInfos/create'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/votes/:vote/options',
    name: 'options.index',
    component: resolve => require(['@/components/options/Index'], resolve),
    meta: {}
  },
  {
    path: '/votes/:vote/options/:option/edit',
    name: 'options.edit',
    component: resolve => require(['@/components/options/edit'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/votes/:vote/options/create',
    name: 'options.create',
    component: resolve => require(['@/components/options/create'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/rewardItems',
    name: 'rewardItems.index',
    component: resolve => require(['@/components/rewardItems/Index'], resolve),
    meta: {}
  },
  {
    path: '/rewardItems/:rewardItem/edit',
    name: 'rewardItems.edit',
    component: resolve => require(['@/components/rewardItems/edit'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/rewardItems/create',
    name: 'rewardItems.create',
    component: resolve => require(['@/components/rewardItems/create'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/rankinglists',
    name: 'rankinglists.index',
    component: resolve => require(['@/components/rankinglists/Index'], resolve),
    meta: {}
  },
  {
    path: '/rankinglists/:reward_item/edit',
    name: 'rankinglists.edit',
    component: resolve => require(['@/components/rankinglists/edit'], resolve),
    meta: {requiresAuth: true}
  },
  {
    path: '/rankinglists/create',
    name: 'rankinglists.create',
    component: resolve => require(['@/components/rankinglists/create'], resolve),
    meta: {requiresAuth: true}
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    if (Store.state.AuthUser.authenticated || jwtToken.getToken()) {
      return next()
    } else {
      return next({'name': 'login'})
    }
  }
  if (to.meta.requiresGuest) {
    if (Store.state.AuthUser.authenticated || jwtToken.getToken()) {
      return next({'name': 'home'})
    } else {
      return next()
    }
  }
  next()
})

export default router
